package com.vframework.common.aspect;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.utils.IPUtils;
import com.vframework.common.response.R;
import com.vframework.filter.TokenFilter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Aspect
@Component
@Slf4j
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    /**
     * 定义切点
     */
    @Pointcut("execution(* com.vframework.system.*.controller..*(..)) || execution(* com.admin.*.controller..*(..))")
    public void requestServer() {
    }

    @Around("requestServer()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        //记录请求开始执行时间：
        long beginTime = System.currentTimeMillis();
        //获取请求信息
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();

        //获取代理地址、请求地址、请求类名、方法名
        String remoteAddress = IPUtils.getClientIpAddr(request);
        String requestURI = request.getRequestURI();
        String methodName = pjp.getSignature().getName();
        String requestHeader = request.getHeader(TokenFilter.TOKEN_KEY);
        if (StrUtil.isEmpty(requestHeader)) {
            requestHeader = request.getHeader(TokenFilter.AUTHORIZATION);
        }
        String clazzName = pjp.getTarget().getClass().getSimpleName();

        //获取请求参数：
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        //获取请求参数类型
        String[] parameterNames = ms.getParameterNames();
        //获取请求参数值
        Object[] parameterValues = pjp.getArgs();
        StringBuilder sb = new StringBuilder();
        //组合请求参数，进行日志打印
        if (parameterNames != null && parameterNames.length > 0) {
            for (int i = 0; i < parameterNames.length; i++) {
                if (parameterNames[i].equals("bindingResult")) {
                    break;
                }
                if ((parameterValues[i] instanceof HttpServletRequest) || (parameterValues[i] instanceof HttpServletResponse)) {
                    sb.
                            append("[").
                            append(parameterNames[i]).append("=").append(parameterValues[i])
                            .append("]");
                } else {
                    sb.
                            append("[").
                            append(parameterNames[i]).append("=")
                            .append(JSON.toJSONString(parameterValues[i], SerializerFeature.WriteDateUseDateFormat))
                            .append("]");
                }
            }
        }
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            //请求操纵失败  记录错误日志
            StringBuilder errLogSb = new StringBuilder();
            errLogSb.append("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>(ง•̀_•́)ง (っ•̀ω•́)っ>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
                    .append("切面处理请求错误！\n")
                    .append("IP信息(ง•̀_•́)ง->： 【{}】\n")
                    .append("URI信息(ง•̀_•́)ง->：【{}】\n")
                    .append("请求映射控制类(ง•̀_•́)ง->：【{}】\n")
                    .append("请求方法(ง•̀_•́)ง->：【{}】\n")
                    .append("请求令牌(◍'౪`◍)ﾉﾞ->：【{}】\n")
                    .append("请求参数列表(ง•̀_•́)ง->：【{}】\n")
                    .append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            logger.error(errLogSb.toString(), remoteAddress, requestURI, clazzName, methodName, requestHeader, sb.toString());
            throw throwable;
        }
        //请求操作成功
        String resultJosnString = "";
        if (result != null) {
            if (result instanceof R) {
                ObjectMapper mapper = new ObjectMapper();
                resultJosnString = mapper.writeValueAsString(result);
            } else {
                resultJosnString = String.valueOf(result);
            }
        }
        //记录请求完成执行时间：
        long endTime = System.currentTimeMillis();
        long usedTime = endTime - beginTime;
        //记录日志
        StringBuilder logSb = new StringBuilder();
        logSb.append("\n--------------------------------------------------------------------------------------------------------------------------------------------------------\n")
                .append("请求操作成功！ 请求耗时：【{}】\n")
                .append("IP信息(◍'౪`◍)ﾉﾞ->： 【{}】\n")
                .append("URI信息(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("请求映射控制类(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("请求方法(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("请求令牌(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("请求参数列表(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("返回值(◍'౪`◍)ﾉﾞ->：【{}】\n")
                .append("--------------------------------------------------------------------------------------------------------------------------------------------------------");
        logger.info(logSb.toString(), usedTime, remoteAddress, requestURI, clazzName, methodName, requestHeader, sb.toString(), resultJosnString);
        return result;
    }
}
